package com.bdsy.business.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.bdsy.business.dao.ShopIdentInfoMapper;
import com.bdsy.business.model.ShopIdentInfo;
import com.bdsy.business.service.ShopIdentInfoService;
import com.bdsy.common.utils.ZipUtil;

@Service("shopIdentInfoService")
public class ShopIdentInfoServiceImpl implements ShopIdentInfoService{

	@Autowired
	ShopIdentInfoMapper shopIdentInfoMapper;
	
	@Override
	public boolean insert(MultipartFile file,ShopIdentInfo shopIdentInfo) {
		
		//服务器项目目录路径
		String path = "/usr/local/tomcat/webapps/adicms_image/idemage";
//		String path = "E:\\Program Files\\Apache Software Foundation\\apache-tomcat-8.5.23\\webapps\\adicms_image\\idemage";

		if(file !=null){
			//文件类型
			String contentType = file.getContentType();
			//获得文件后缀名   
	        String suffixName=contentType.substring(contentType.indexOf("/")+1); 
	        //生成uuid作为文件名称    
	        String uuid = UUID.randomUUID().toString().replaceAll("-","");    
	        //得到 文件名  
	        String fileName=uuid+"."+suffixName;  
	        
	        System.out.println(path+"--***---"+contentType+"--***---"+suffixName+"---***---"+fileName);
			//生成文件
		    File dir = new File(path, fileName);  
	        if(!dir.exists()){  
	            dir.mkdirs();  
	        }   
	        System.out.println(dir.getAbsolutePath()+"///////");
	        try {
				file.transferTo(dir);
			}catch (IOException e) {
				System.out.println("文件转换失败");
				e.printStackTrace();
			}
	        
	        //解压缩上传的照片
	        File upload = new File(path);//解压文件存储的路径
			List<File> list = ZipUtil.upzipIdentFile(dir, upload.getAbsolutePath());
			//删除压缩文件
			ZipUtil.deletefile(dir.toString());
			System.out.println(list+"///////-----");
			
			String imageUrl = "";
			for (File file2 : list) {
				imageUrl += file2.toString()+";";
			}
			shopIdentInfo.setIdentImg(imageUrl);
		}
		shopIdentInfo.setId((int)((Math.random()*9+1)*10000000));
		int result = shopIdentInfoMapper.insert(shopIdentInfo);
		return result>0?true:false;
	}

	@Override
	public List<Map> shopIdentInfoList(int shopId) {
		List<Map> shopIdentInfoList = shopIdentInfoMapper.shopIdentInfoList(shopId);
		return shopIdentInfoList;
	}

	@Override
	public List<Map> shopIdentInfoLists() {
		return shopIdentInfoMapper.shopIdentInfoLists();
	}

}
